/**
 * 初始化整体客户图标
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/scatter');
    var zrnumber = require('echarts/util/number');
    var myChart = ec.init(document.getElementById('inventory_consumeRate')); 
      
    var curcoordinate='1';      
    var nameMap1 = {"1":"%","2":"天"};
    var nameMap2 = {"1":"充值","2":"消耗"};
    var nameMap3 = {"1":"消耗率","2":"消耗周期"};
    
    var option = {
    title : {
    },
    tooltip : {
        trigger: 'axis',
        showDelay : 0,
        formatter : tooltipFunc,
        axisPointer:{
            show: true,
            type : 'cross',
            lineStyle: {
                type : 'dashed',
                width : 1
            }
        }
    },
    legend: {
    	show: false,
        data:['类别']
    },
    toolbox: {
        show : true,
        orient:'vertical',
        feature : {
            mark : {show: true},
            dataZoom : {show: true},
            dataView : {show: true, readOnly: false},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    xAxis : [
        {
            type : 'value',
            scale:true,
            axisLabel : {
                formatter: '{value} 元'
            }
        }
    ],
    yAxis : [
        {
            type : 'value',
            scale:true,
            axisLabel : {
                formatter: '{value}'+nameMap1[curcoordinate]
            }
        }
    ],
    series : [
        {
            name:'类别',
            type:'scatter',
            itemStyle: {
    	    	normal: {
    	    		color:['#30e0e0'],
    	        	label: {
    	                        show:true,
    	                        formatter: nametipFunc	                    
    	                    }
    	                  
    	                }          
    	            },
            data: [],
            markLine : {
                data : [
                    {type : 'average', name: '平均值'}
                ]
            }
        }
    ]
};
 var months = [];

    /**
     * 根据指标显示日数据
     * @param eventType= 2:第一次加载
     */
    function show(eventType){
        var cityName = $('#cityName').val(); 
        var businessName = $('#businessName').val(); 
        var beginDate = $('#beginDate_consumeRate').val();
        var vipType = $('#vipType_consumeRate').val(); 
        var indexType = $('#indexType_consumeRate').val();
    
        curcoordinate=indexType;
        myChart.showLoading({ text: '正在努力的读取数据中...' });
        $.ajax({
            url: '/dashboard/homePage/ajax/consume_rate',
            type: 'post',
            async: true,
            data:{"businessName":businessName,"vipType":vipType,"cityName":cityName,"beginDate":beginDate,"indexType":indexType},
            dataType: 'json',
            timeout: 5000,
            success: function(data, textStatus) {
        
				option.series[0].data=data.relist;			
				option.yAxis[0].axisLabel.formatter='{value}'+nameMap1[curcoordinate];
				
				months = data.months;
				
				var input=$('#beginDate_consumeRate');
   		        setDatePicker(EC, $('#consumeRate_option').attr('statType'),input);
								
            	myChart.setOption(option,true);          	
            	myChart.hideLoading(); 	
	            if(data.relist.length==0)
	            {
	                 myChart.showLoading({
                      	text: '您好，该日暂无数据...'
                        });
	             }                             
            },
            error : function() {
                myChart.hideLoading();
            }
        });
    }
    
    //标示框
    function  nametipFunc(params, ticket, callback,showType){
		  var showvalue=params.value[4];
		  return showvalue;
         
    }   
    
    //提示框
    function  tooltipFunc(params, ticket, callback,showType){
    	 if (params.value.length > 1) {
         	var showvalue='<table class="table-center">';
         	showvalue+='<tr><td>'+params.value[4]+'</td></tr>';
     
            var s=params.value[2].toFixed(2);
            if(s>0){
                s='<td style="color: orangered;">'+s+'%&#8593</td>';
            }else{
                s='<td style="color: greenyellow;">'+s+'%&#8595</td>';
            }      
            showvalue+='<tr><td>'+nameMap2[curcoordinate]+'：</td><td>'+ zrnumber.addCommas(params.value[0].toFixed(0)) + '</td><td>'+' 环比：</td>'+ s+' </tr>';
                 
            var s=params.value[3].toFixed(2);
            if(s>0){
                s='<td style="color: orangered;">'+s+'%&#8593</td>';
             }else{
                s='<td style="color: greenyellow;">'+s+'%&#8595</td>';
             }
             if(curcoordinate==1)
             {
             	 showvalue+='<tr><td>'+nameMap3[curcoordinate]+'：</td><td>'+ params.value[1].toFixed(2) + '('+nameMap1[curcoordinate]+')</td><td>'+' 环比：</td>'+ s+' </tr>';
             }
             else
              	showvalue+='<tr><td>'+nameMap3[curcoordinate]+'：</td><td>'+ params.value[1].toFixed(0) + '('+nameMap1[curcoordinate]+')</td><td>'+' 环比：</td>'+ s+' </tr>';
              
             return showvalue;
         }
    }   

   function formatShowDate(yyyymmdd){
        var statType = $('#consumeRate_option').attr('statType');
        if('day'==statType){
            return yyyymmdd;
        }else if('week'==statType){
            return yyyymmdd;
        }else if('month'==statType){
            return yyyymmdd.substr(0,4)+"年"+yyyymmdd.substr(5,2)+"月";
        }
    }

    var zrUtil = require('zrender/tool/util');
    // console.log(zrUtil);

    function getIndexByName(data,name) {
        var dataLength = data.length;
        for (var i = 0; i < dataLength; i++) {
            if (data[i] == name || typeof data[i].value != 'undefined' && data[i].value == name) {
                return i;
            }
        }
        return -1;
    }

    function formatTime(time){
        return time;
    }  
    function getShowDays(){
        return months;
    }
    
    function getAllDays(){
        return months;
    }

    var EC={
        show:show,
        getShowDays:getShowDays,
        getAllDays:getAllDays,
        resize:function(){myChart.resize()}  
    };  
      
    //绑定书刷新按钮点击事件
    $("#refresh_consumeRate_btn").click(function(){
        var type = 2;      
        EC.show(type);
    });
   
    //时间选择事件
    $('#beginDate_consumeRate').datepicker().on('changeDate',function(e){
//    	var type = 2;      
//        EC.show(type);
    });

    return EC;
});

